package com.cskaoyan.controller.stage;

import com.cskaoyan.exception.StockValidationException;
import com.cskaoyan.model.bo.CartBO;
import com.cskaoyan.model.bo.CheckOutOrderBO;
import com.cskaoyan.model.vo.*;
import com.cskaoyan.service.CartService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @program: mall38
 * @description: 购物车模块
 * @author: Lexis
 * @create: 2022-05-08 22:11
 **/

@RestController
@RequestMapping("wx/cart")
public class WxCartController {
    @Autowired
    CartService cartService;

    @GetMapping("index")
    /**
     * @author: lixiaosheng
     * @description: 显示购物车列表
     * @date: 2022/5/9 10:46
     */
    public BaseRespVO<CartVO> index() {
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipals().getPrimaryPrincipal();
        CartVO cartVO = cartService.queryCartInfo(username);
        return BaseRespVO.ok(cartVO);
    }

    @PostMapping("checked")
    /**
     * @author: lixiaosheng
     * @description: check订单
     * @date: 2022/5/9 12:26
     */
    public BaseRespVO<CartVO> check(@RequestBody CartBO cartBO) {
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipals().getPrimaryPrincipal();
        cartService.updateCheckedByGoodsId(cartBO, username);
        CartVO cartVO = cartService.queryCartInfo(username);
        return BaseRespVO.ok(cartVO);
    }

    @PostMapping("update")
    /**
     * @author: lixiaosheng
     * @description: 更新商品数量
     * @date: 2022/5/9 12:27
     */
    public ExceptionVO update(@RequestBody UpdateOrderVO updateOrderVO) {
        cartService.updateOrderById(updateOrderVO);
        return ExceptionVO.fine();
    }

    @PostMapping("delete")
    /**
     * @author: lixiaosheng
     * @description: 删除订单
     * @date: 2022/5/9 12:27
     */
    public BaseRespVO<CartVO> delete(@RequestBody Map map) {
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipals().getPrimaryPrincipal();
        List<Integer> productIds = (List<Integer>) map.get("productIds");
        CartVO cartVO = cartService.deleteOrderByProductId(productIds, username);
        return BaseRespVO.ok(cartVO);
    }

    @GetMapping("checkout")
    /**
     * @author: lixiaosheng
     * @description: 填写订单
     * @date: 2022/5/9 13:23
     */
    public BaseRespVO<CheckOutOrderVO> checkout(CheckOutOrderBO check) {
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipals().getPrimaryPrincipal();
        CheckOutOrderVO checkOutOrderVO = cartService.checkOutOrder(check, username);
        return BaseRespVO.ok(checkOutOrderVO);
    }

    @GetMapping("goodscount")
    public BaseRespVO<Integer> goodscount() {
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return BaseRespVO.ok(0);
        }
        String username = (String) subject.getPrincipals().getPrimaryPrincipal();
        Integer goodsCount = cartService.queryCartGoodsCountByUsername(username);
        if (goodsCount == null) {
            goodsCount = 0;
        }
        return BaseRespVO.ok(goodsCount);
    }

    @PostMapping("add")
    public ExceptionVO add(@RequestBody Map map) throws StockValidationException {
        int goodsId = (int) map.get("goodsId");
        int number = (int) map.get("number");
        int productId = (int) map.get("productId");
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return ExceptionVO.error(501, "请先登录");
        }
        String username = (String) subject.getPrincipals().getPrimaryPrincipal();
        cartService.add(username, goodsId, number, productId);
        Integer goodsCount = cartService.queryCartGoodsCountByUsername(username);
        if (goodsCount == null) {
            goodsCount = 0;
        }
        return BaseRespVO.ok(goodsCount);
    }

    @PostMapping("fastadd")
    public ExceptionVO fastadd(@RequestBody Map map) throws StockValidationException {
        int goodsId = (int) map.get("goodsId");
        int number = (int) map.get("number");
        int productId = (int) map.get("productId");
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return ExceptionVO.error(501, "请先登录");
        }
        String username = (String) subject.getPrincipals().getPrimaryPrincipal();
        Integer cartId = cartService.add(username, goodsId, number, productId);
        return BaseRespVO.ok(cartId);
    }
}
